// pages/thaw/thaw.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: false,
    select1: false,
    select2: false,
    minApplyTime: '查询时间',
    status: '所有状态',
    thaw: '手机号码',
    cancelType: ['所有状态', '待处理', '已解冻', '已拒绝'],
    type: ['手机号码', '订单编号'],
    opition: false,
    hasHandleCount: '',
    detailsList: [],
    nameType: '解冻申请',
    color: '',
    colors: '',
    inputValue: '', //input初始值
    // 时间选择器
    currentDate: new Date().getTime(),
    minDate: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '解冻申请',
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //渲染数据 
    this.checked()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 下拉框选项
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  // 选择日期
  onInput(event) {
    this.setData({
      currentDate: event.detail
    });
  },
  //时间选择器确定按钮
  closePicker(e) {
    var data = app.dateFormats(e.detail)
    this.setData({
      minApplyTime: data,
      select: false
    });
  },
  //时间选择器取消按钮
  closePickers() {
    this.setData({
      minApplyTime: '查询时间',
      select: false
    });
  },
  // 解冻状态下拉框选项
  bindShowMsg1() {
    this.setData({
      select1: !this.data.select1
    })
  },
  mySelect1(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      status: name,
      select1: false
    })
  },
  // 解冻下拉框
  bindShowThaw() {
    this.setData({
      opition: !this.data.opition
    })
  },
  myOpition(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      thaw: name,
      opition: false
    })
  },
  // 输入框
  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  // 解冻申请点击事件
  screen(e) {
    this.setData({
      nameType: e.currentTarget.dataset.name,
      color: '#ada16b',
      colors: ''
    })
    wx.request({ //获取解冻申请列表
      url: app.url + '/user/auth3/applyUnfreeze/findUnFreezeList',
      method: 'post',
      header: {
        token: wx.getStorageSync('token'),
      },
      data: {
        projectId: wx.getStorageSync('id'),
      },
      success: (res) => {
        if (res.data.code == 0) {
          // 调取成功，渲染数据
          this.setData({
            detailsList: res.data.data.records || [],
            notHandleCount: res.data.data.total,
          })
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  // 已解冻点击事件
  screens(e) {
    this.setData({
      nameType: e.currentTarget.dataset.name,
      colors: '#ada16b',
      color: ''
    })
    wx.request({ //获取解冻申请列表
      url: app.url + '/order/auth3/applyUnfreeze/hasHandleUnFreezeList',
      method: 'post',
      header: {
        token: wx.getStorageSync('token'),
      },
      data: {
        projectId: wx.getStorageSync('id'),
      },
      success: (res) => {
        if (res.data.code == 0) {
          // 调取成功，渲染数据
          if (res.data.data != null) {
            this.setData({
              hasHandleCount: res.data.data.total
            })

            var detailsList = res.data.data.records || []
            if (detailsList.length != 0) {
              for (var i = 0; i < detailsList.length; i++) {
                detailsList[i].createTime = app.dateFormat(res.data.data.records[i].createTime)
              }
            }
            this.setData({
              detailsList: detailsList
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          });
        }
      }
    })
  },
  //搜索
  checked() {
    wx.showLoading({
      title: '加载中..',
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 1000)
    var time = '';
    if (this.data.minApplyTime != '查询时间') {
      // time = app.newDate(this.data.minApplyTime)
      time =this.data.minApplyTime
    } else {
      time = ''
    }
    if (this.data.nameType == '解冻申请') {
      this.getindex()
      wx.request({ //获取解冻申请列表
        url: app.url + '/user/auth3/applyUnfreeze/findUnFreezeList',
        method: 'post',
        header: {
          token: wx.getStorageSync('token'),
        },
        data: {
          projectId: wx.getStorageSync('id'),
          maxApplyTime: time,
          mobile:this.data.inputValue,
        },
        success: (res) => {
          if (res.data.code == 0) {
            // 调取成功，渲染数据
            this.setData({
              detailsList: res.data.data.records || [],
              notHandleCount: res.data.data.total,
            })
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })

    } else if (this.data.nameType == '解冻记录') {

      wx.request({ //获取解冻申请列表
        url: app.url + '/order/auth3/applyUnfreeze/hasHandleUnFreezeList',
        method: 'post',
        header: {
          token: wx.getStorageSync('token'),
        },
        data: {
          projectId: wx.getStorageSync('id'),
          maxApplyTime: time,
          mobile: this.data.inputValue,
        },
        success: (res) => {
          if (res.data.code == 0) {
            // 调取成功，渲染数据
            if (res.data.data!=null){
              this.setData({
                hasHandleCount: res.data.data.total
              })

              var detailsList = res.data.data.records || []
              if (detailsList.length != 0) {
                for (var i = 0; i < detailsList.length; i++) {
                  detailsList[i].createTime = app.dateFormat(res.data.data.records[i].createTime)
                }
              }
              this.setData({
                detailsList: detailsList
              })
            }
            
            
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            });
          }
        }
      })


    }

  },
  // 确认解冻
  sure(e) {
    var projectId = e.currentTarget.dataset.projectid
    var userId = e.currentTarget.dataset.userid
    wx.showModal({
      title: '是否解冻该用户冻结金',
      success: res => {
        if (res.confirm) {
          wx.request({
            url: app.url + '/user/auth3/userMoney/unfreezeUserMoney',
            method: 'POST',
            header: {
              token: wx.getStorageSync('token'),
            },
            data: {
              projectId: projectId,
              userId: userId
            },
            success: (res) => {
              if (res.data.code == 0) {
                this.checked()
                wx.showToast({
                  title: '解冻成功',
                })
              } else {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: res.data.msg
                });
              }
            }
          })
        }
      }
    })

  },
  // 拒绝解冻
  refuse(e) {
    var projectId = e.currentTarget.dataset.projectid
    var userId = e.currentTarget.dataset.userid
    wx.request({
      url: app.url + '/user/auth3/userMoney/unfreezeUserMoney',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        token: wx.getStorageSync('token'),
      },
      data: {
        freezeStatus: 2,
        projectId: projectId,
        userId: userId
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.checked()
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          });
        }
      }
    })
  },
  // 获取解冻记录条数
  getindex(e) {
    wx.request({
      url: app.url + '/order/auth3/applyUnfreeze/hasHandleUnFreezeListTotal',
      method: 'POST',
      header: {
        token: wx.getStorageSync('token'),
      },
      data: {
        projectId: wx.getStorageSync('id'),
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            hasHandleCount: res.data.data
          })
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          });
        }
      }
    })
  }
})